package com.Xht.www.servlet;

//后端响应管理员查询状态为申请中的商家的请求

import com.Xht.www.dao.MerchantInfoDAO;
import com.Xht.www.po.MerchantInfoPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/PendingShopApplicationsServlet")
public class PendingShopApplicationsServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");

        MerchantInfoDAO dao = new MerchantInfoDAO();
        // 查询所有申请状态为"申请中"的商家
        List<MerchantInfoPO> applications = dao.getPendingApplications();

        // 将对象列表转换为JSON并输出
        StringBuilder jsonResult = new StringBuilder("[");
        for (int i = 0; i < applications.size(); i++) {
            MerchantInfoPO app = applications.get(i);
            jsonResult.append("{")
                    .append("\"merchantId\":").append(app.getMerchantId()).append(",")
                    .append("\"merchantName\":\"").append(escapeJson(app.getMerchantName())).append("\",")
                    .append("\"address\":\"").append(escapeJson(app.getAddress())).append("\",")
                    .append("\"establishmentYear\":").append(app.getEstablishmentYear()).append(",")
                    .append("\"contactInfo\":\"").append(escapeJson(app.getContactInfo())).append("\",")
                    .append("\"keywords\":\"").append(escapeJson(app.getKeywords())).append("\"")
                    .append("}");

            if (i < applications.size() - 1) {
                jsonResult.append(",");
            }
        }
        jsonResult.append("]");

        response.getWriter().write(jsonResult.toString());
    }

    // 简单的JSON转义方法
    private String escapeJson(String input) {
        if (input == null) return "";
        return input.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }
}
